package com.atguigu.spzx.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsConfigurationSource;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

@Configuration
public class SpzxGatewayConfig {
    //注册跨域过滤器
    @Bean
    public CorsWebFilter corsWebFilter(){
        UrlBasedCorsConfigurationSource corsConfigurationSource = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.addAllowedMethod("*");
        config.addAllowedHeader("*");
        config.addAllowedOrigin("*"); // 表示所有的客户端服务器都可以跨域访问 www.spzx.com
        //配置 /**的跨域
        corsConfigurationSource.registerCorsConfiguration("/**" , config);

        return new CorsWebFilter(corsConfigurationSource);
    }
}
